package com.tigase.controller;

/**
 * Created by Mer on 2017/7/6.
 */
import java.util.*;

import com.tigase.bean.TigaseUser;
import com.tigase.bean.User;
import com.tigase.service.UserService;
import com.tigase.serviceImpl.UserServiceImpl;
import com.tigase.utils.MD5;
import io.swagger.annotations.*;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.chain.contexts.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author tianfeng
 * @version 1.0.0
 *
 */
@RestController
@RequestMapping(value="/user")     // 通过这里配置使下面的映射都在/users下，可去除
public class UserController {
    @Autowired
    private UserService UserService;

    @RequestMapping(value="/zhuce")
    public Map<String,Object> getUserList(@RequestBody TigaseUser user ,HttpServletResponse HttpServletResponse) {
        HttpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
        Map<String,Object> map =new HashMap<>();
     if(!StringUtils.isEmpty(user.getUserName())&&user.getUserName()!=""&&user.getUserPass()!=""&&!StringUtils.isEmpty(user.getUserPass())){
       TigaseUser user1= UserService.registerUser(user.getUserName());
       if(null!=user1.getUserName()){
           map.put("msg","用户名已存在");
           map.put("flag",false);
       }else{
          int a=UserService.insertUser(user);
           if(a>0){
               Map<String,String> result=new HashMap<>();
               result.put("userName",user.getUserName());
               result.put("passWorld",MD5.GetMD5Code(user.getUserPass()));
               map.put("msg","注册成功");
               map.put("result",result);
               map.put("flag",true);
           }else{
               map.put("msg","注册失败");
               map.put("flag",false);
           }
       }
     }else{
         map.put("msg","用户不允许为空");
         map.put("flag",false);
     }
        return  map;

    }
    /**
    *@Author tianfeng
    *@Date 2017/7/20 11:27
    */
    @RequestMapping(value="/login")
    public Map<String,Object> userLogin(@RequestBody TigaseUser user,HttpServletResponse HttpServletResponse) {
        HttpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
        Map<String,Object> map =new HashMap<>();
        if(!StringUtils.isEmpty(user.getUserName())||!StringUtils.isEmpty(user.getUserPass())){
            TigaseUser userw= UserService.registerUser(user.getUserName());
            System.out.println(userw+"：dsdff "+userw.getUserPass());
            if(null==userw.getUserName()){
                map.put("msg","账号不存在,请核对后输入");
                map.put("flag",false);
                return map;
            }
            if(((userw.getUserName()).equals(user.getUserName()))&&((userw.getUserPass()).equals(MD5.GetMD5Code(user.getUserPass())))){
                if(userw.getAccountStatus()!=1){
                    map.put("msg","您的账号已注销，请联系管理员处理");
                    map.put("flag",false);
                }else{
                    Map<String,String> result=new HashMap<>();
                    result.put("userName",user.getUserName());
                    result.put("passWorld",MD5.GetMD5Code(user.getUserPass()));
                    map.put("msg","登陆成功");
                    map.put("result",result);
                    map.put("flag",true);
                }
            }else{
                map.put("msg","用户名密码错误");
                map.put("flag",false);
            }

        }else{
            map.put("msg","用户名密码不允许为空");
            map.put("flag",false);
        }
        return  map;
    }


}